﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Text;

namespace SimpleIpc
{
    public class TcpRemoteChannel : AbstractRemoteChannel
    {
        private string name;

        public override string Name => name;

        public TcpRemoteChannel(string name)
            : this(name, 0)
        {

        }
        /// <summary>
        /// ipc信道
        /// </summary>
        /// <param name="name">信道名称</param>
        /// <param name="port">tcp端口</param>
        public TcpRemoteChannel(string name, int port)
        {
            this.name = name;
            IDictionary props = new Hashtable();
            props["name"] = name;
            props["port"] = port;
            CreateChannel(props, new BinaryClientFormatterSinkProvider(), new BinaryServerFormatterSinkProvider());
        }

        public TcpRemoteChannel(IDictionary properties, IClientChannelSinkProvider clientSinkProvider, IServerChannelSinkProvider serverSinkProvider)
        {
            CreateChannel(properties, clientSinkProvider, serverSinkProvider);
        }

        private void CreateChannel(IDictionary properties, IClientChannelSinkProvider clientSinkProvider, IServerChannelSinkProvider serverSinkProvider)
        {
            try
            {
                channel = new TcpChannel(properties, clientSinkProvider, serverSinkProvider);
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
